package com.yzycoc.coc.gf.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.yzycoc.coc.gf.constant.ApiUrlConstant;
import com.yzycoc.coc.gf.enums.KeyValues;
import com.yzycoc.coc.gf.info.CocVersion;
import com.yzycoc.coc.gf.info.LexiconInfo;
import com.yzycoc.coc.gf.service.CocService;
import com.yzycoc.coc.gf.util.ConfigFileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 初始话配置
 * 创建时间： 2023/9/7
 *
 * @author yzy
 * @version 1.0
 **/
@Slf4j
public class StartConfig {
    /**
     * 更新配置文件
     */
    public static void startConfig() {
        String url = ApiUrlConstant.CONFIG.getConfig("gf_url");
        if (StrUtil.isNotEmpty(url)) {
            CocConfig.API_URL = url;
        } else {
            CocConfig.API_URL = KeyValues.URL.getConfig();
            ApiUrlConstant.CONFIG.setConfig("gf_url", CocConfig.API_URL);
        }


        //获取开始强制会员
        //ConfigFileUtil configFileUtil = new ConfigFileUtil("config/config.conf");
        //VipConfig.SYSTEM_STATE = configFileUtil.getBoolean("system_vip");
        //if (VipConfig.SYSTEM_STATE == null) {
        //    VipConfig.SYSTEM_STATE = false;
        //}
        //try {
        //    initContactInformation();
        //    VipConfig.startGroupAvgVip();
        //}catch (Exception e){
        //    e.printStackTrace();
        //}

    }

    /**
     * 同步词库
     */
    public static void startLexicon() {
        String config = KeyValues.SWITCH_LEXICON.getConfig();
        if (!"true".equals(config)) {
            return;
        }
        try {
            CocVersion version
                    = CocService.httpLexiconVersion("LEXICON");
            if (version == null || version.getVersion() == null) {
                log.error("异常：无法获取部落冲突指导词库！https://www.cocservice.top");
                return;
            }
            if (CocConfig.LEXICON_VERSION != null && version.getVersions().equals(CocConfig.LEXICON_VERSION)) {
                return;
            }
            CocConfig.LEXICON_VERSION = version.getVersions();
            List<LexiconInfo> lexiconInfos = CocService.httpLexicon();
            if (CollectionUtil.isEmpty(lexiconInfos)) {
                log.error("异常：无法获取部落冲突指导词库！https://www.cocservice.top");
            }
            CocConfig.lexiconInfo = new HashMap<>();

            CocConfig.lexiconInfo = lexiconInfos.stream().collect(Collectors.groupingBy(LexiconInfo::getAntistop));
            log.info("部落冲突指导数据同步成功，同步词库{}条！https://www.cocservice.top", CocConfig.lexiconInfo.size());
        } catch (Exception e) {
            log.error("部落冲突指导词库同步失败！");
            e.printStackTrace();
        }
    }

    /**
     * 初始化联系方式
     */
    private static void initContactInformation() {
        String contactFilePath = System.getProperty("user.dir") + "/data/config/contact_info.conf";
        File contactFile = new File(contactFilePath);
        // 如果文件不存在，则创建文件并写入默认值
        if (!contactFile.exists()) {
            FileUtil.touch(contactFile); // 创建空文件
        }
        // 读取文件内容
        String contactInfo = FileUtil.readString(contactFile, StandardCharsets.UTF_8);

        if (StrUtil.isNotEmpty(contactInfo)) {
            VipConfig.CONTACT_INFORMATION = contactInfo;
        } else {
            // 文件存在但内容为空，写入默认值
            FileUtil.writeString(VipConfig.CONTACT_INFORMATION, contactFile, StandardCharsets.UTF_8);
        }
    }
}
